package com.sunshine.lnuplus.ui.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.DialogInterface
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.KeyEvent
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.adapter.LoadFileAdapter
import com.sunshine.lnuplus.base.BaseActivity
import com.sunshine.lnuplus.callback.LoadLessonFileCallBack
import com.sunshine.lnuplus.callback.LoginCallBack
import com.sunshine.lnuplus.utils.LoadLessonUtils
import com.sunshine.lnuplus.utils.StatusBarUtils
import com.sunshine.lnuplus.utils.TagHelper
import com.sunshine.lnuplus.view.DialogWithoutView
import com.sunshine.lnuplus.viewModel.LoginViewModel
import kotlinx.android.synthetic.main.activity_load_file_lesson_choose.*

class LoadFileLessonChooseActivity : BaseActivity() {

    private lateinit var myAdapter: LoadFileAdapter
    private lateinit var loginViewModel: LoginViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_load_file_lesson_choose)

        loginViewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
        initView()
    }

    private fun initView() {
        title = "导入班级课表"
        StatusBarUtils.setStatusBarWithColor(this, color)
        val dialog = DialogWithoutView.newInstance("请注意", resources.getString(R.string.load_warn), false, DialogInterface.OnClickListener { _, _ ->  }, null)
        dialog.show(supportFragmentManager, "loadWarn")
        myAdapter = LoadFileAdapter(this, object : LoadLessonFileCallBack{
            override fun onSuccess(myLessonList: ArrayList<Map<String, String>>) {
                LoadLessonUtils.initFileLesson(loginViewModel.repository, object : LoginCallBack{
                    override fun onSuccess() {
                        handler.sendEmptyMessage(1)
                    }

                    override fun onFail(status: String) {
                        handler.sendEmptyMessage(2)
                    }

                },myLessonList)
            }

        })

        load_recycler.apply {
            layoutManager = LinearLayoutManager(this@LoadFileLessonChooseActivity)
            addItemDecoration(DividerItemDecoration(this@LoadFileLessonChooseActivity, DividerItemDecoration.VERTICAL))
            adapter = myAdapter
        }

        sidebar.setOnTouchLetterChangeListener {
            val pos = myAdapter.getIndex(it)
            if(pos != -1){
                load_recycler.scrollToPosition(pos)
                val layoutManager = load_recycler.layoutManager as LinearLayoutManager
                layoutManager.scrollToPositionWithOffset(pos, 0)
            }
        }

        getToolBar()?.setNavigationOnClickListener {
            setResult(Activity.RESULT_CANCELED)
            supportFinishAfterTransition()
        }

    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK && event?.action == KeyEvent.ACTION_DOWN) {
            setResult(Activity.RESULT_CANCELED)
            finish()
            true
        } else {
            super.onKeyDown(keyCode, event)
        }
    }

    @SuppressLint("HandlerLeak")
    val handler = object : Handler(){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when(msg.what){
                1 -> {
                    setResult(TagHelper.LOGIN)
                    finish()
                }
                2 -> {
                    setResult(Activity.RESULT_CANCELED)
                    finish()
                }
            }
        }
    }
}
